<template>
    <div>
        <div class="blackbg"></div>
        <div class="cart">
            <div class="c-info">
                <div class="c-p">
                    <div class="c-p-img">
                        <img :src="cart.pic"/>
                    </div>
                    <div class="c-p-name">
                        <p>{{cart.name}}</p>
                        <p class="g-price"><span class="g-price-tag">会员价：￥</span>{{cart.price}}</p>
                    </div>
                    <div class="c-p-icon" @click="cancelBookOrder">
                        <img src=""/>
                    </div>
                </div>
                <div class="c-item">
                    <div class="name number">
                        <span>购买数量:</span>
                        <span>
                            <input type="number" v-model="cartnumber" class="num" @blur="checkCartNumber"/>
                        </span>
                    </div>
                    <div class="number">
                        <!--<span class="delete" :class="cart.goodsCount === cart.lowestOrderCount ? 'disabled' : ''" @click="addNum(-1)"></span>-->
                        <!--<span class="add" :class="cart.goodsCount === maxcount ? 'disabled' : ''"-->
                              <!--@click="addNum(1)"></span>-->
                    </div>
                </div>
                <!--<div class="c-item">-->
                <!--<p class="name">发货选择</p>-->
                <!--<div  class="delieve">-->
                <!--<div :class="cart.deliverType === 1 ? 'active' : ''" @click="chooseDelivery(1)">平台发货</div>-->
                <!--<div :class="cart.deliverType === 2 ? 'active' : ''" @click="chooseDelivery(2)">代理发货</div>-->
                <!--</div>-->
                <!--</div>-->
                <div class="c-item">
                    <p class="name">地址选择</p>
                    <div v-if="addressList && addressList.length > 0">
                        <div class="address" v-for="(address, index) in addressList" v-if="index === 0"
                             @click="seleceArea(index)" :class="selectedAddId === index ? 'active' : ''"
                             :style="morearea ? 'border-bottom: 1px solid #ddd' : 'border-bottom: none;' ">
                            <span class="default">默认</span>
                            <span class="add-info">{{address.province + address.city + address.region + '   ' + address.detailAddress}}</span>
                            <span class="add-icon" @click="showArea" v-if="addressList.length > 1">
                                <img :class="morearea ? 'rotate': ''"
                                     src=""/>
                            </span>
                        </div>
                        <div v-show="morearea">
                            <div class="address" v-for="(address, index) in addressList" v-if="index > 0"
                                 @click="seleceArea(index)" :class="selectedAddId === index ? 'active' : ''">
                                <span class="dot"></span>
                                <span class="add-info">{{address.province + address.city + address.region + '   ' + address.detailAddress}}</span>
                            </div>
                        </div>
                    </div>
                    <div v-else>
                        <p @click="gotoAddaddress">+ 添加地址</p>
                    </div>
                </div>
            </div>
            <div class="c-footer" @click="bookOrder">
                预定下单
            </div>
        </div>
    </div>
</template>
<script>
  import {ConfirmOrder, wechatPay} from '_common/services/order'
  import {getAllAddress} from '_common/services/address'
  import wxLogin from '_common/wxLogin.js'
  export default {
    props: {
      oricart: {
        type: Object,
        default: {}
      },
      level: {
        type: Number,
        default: 0
      }
    },
    data () {
      return {
        addressList: [],
        maxcount: 100000,
        agencyInfo: {},
        morearea: false, // 是否显示更多地址
        cart: {},
        selectedAddId: 0,
        cartnumber: 0
      }
    },
    created () {
    },
    watch: {
    },
    async beforeMount() {
      await wxLogin(this.getData)
    },
    methods: {
      checkCartNumber () {
        if (this.cartnumber) {
          this.cartnumber = parseInt(this.cartnumber)
          if (this.cartnumber < this.cart.lowestOrderCount) {
            this.$alertTip('最小下單數量為' + + this.cart.lowestOrderCount)
          }
          else if (this.cartnumber > this.maxcount) {
            this.$alertTip('最大下單數量為' + this.maxcount)
          }
          this.cart.goodsCount = this.cartnumber
        }
        else {
          this.cart.goodsCount = this.cartnumber = this.cart.lowestOrderCount
        }
      },
      getData() {
        this.cart = this.oricart
        this.cartnumber = this.cart.goodsCount
        this.getAddressInfo()
      },
      gotoAddaddress () {
        this.$openRouter({
          name: 'add'
        })
      },
      getAddressInfo () {
        getAllAddress().then(res => {
          if (res && res.code === 0) {
            this.addressList = res.data || []
          }
          else {
            this.$alertTip(res.msg || '获取地址失败，请稍后再试')
          }
        }, reject => {
          this.$alertTip('获取地址失败，请稍后再试')
        })
      },
      addNum (flag) {
        if (flag > 0 && this.cart.goodsCount < this.maxcount) {
          this.cart.goodsCount++
        }
        else if (flag < 0 && this.cart.goodsCount > this.cart.agencyInfo.lowestOrderCount) {
          this.cart.goodsCount--
        }
        this.cartnumber = this.cart.goodsCount
      },
      chooseDelivery (flag) {
        this.cart.deliverType = flag
      },
      showArea() {
        this.morearea = !this.morearea
      },
      seleceArea (index) {
        this.selectedAddId = index
      },
      cancelBookOrder () {
        this.$emit('cancelBookOrder')
      },
      bookOrder () {
        if (!this.cart.canOrder) {
          this.$alertTip('很抱歉!您沒有權限進行下單')
          return false
        }
        else {
          let params = {
            addressBookId: this.addressList && this.addressList.length > 0 ? this.addressList[this.selectedAddId].addressId : '',
            goodsCount: this.cart.goodsCount,
            goodsId: this.cart.goodsId,
            deliverType: 0,
            hasConfirmed: true
          }
          if (!params.addressBookId) {
            this.$alertTip('请选择收货地址')
            return false
          }

          if (this.cart.goodsCount < this.cart.lowestOrderCount) {
            this.$alertTip('最小下單數量為' + this.cart.lowestOrderCount)
            return false
          }
          if (this.cart.goodsCount > this.maxcount) {
            this.$alertTip('最大下單數量為' + this.maxcount)
            return false
          }
          let rest = this.cart.goodsCount % 50
          if (rest !== 0) {
            this.$alertTip('訂單數量只能為50的倍數')
            return false
          }
          ConfirmOrder(params).then(res => {
            if (res && res.code === 0) {
              // 调用微信支付弹框
              if (res.data) {
                wechatPay(res.data)
              }
              else {
                this.$openRouter({
                  name: 'myOrder'
                })
              }
            }
            else {
              this.$alertTip(res.msg || '获取失败，请稍后再试')
            }
          }, reject => {
            this.$alertTip('获取失败，请稍后再试')
          })
        }
      }
    }
  }
</script>
<style lang="less">
    @import "../../css/common";
    @import "./shoppingCart";

    .g-price {
        font-size: 48px;
        margin-bottom: 24px;
        color: #ff1515;
        line-height: 1;
        .g-price-tag {
            font-size: 30px;
        }
    }
</style>